﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExJwtBearer;
using RuoVea.ExUtil;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace RuoVea.Api.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("[controller]")]
    [BearerAuthorize]
    public abstract class ApiBaseController : ControllerBase
    {
        #region 请求响应

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        protected virtual ActionResult ToJsonResult(object data)
        {
            return Content(data.Json2Str());
        }

        #region Success 返回成功消息

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <returns></returns>
        protected virtual ActionResult Success()
        {
            return Success("success", null, null, DateTime.Now.Ticks);
        }

 
        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual ActionResult Success( string message)
        {
            return Success( message, null, null, DateTime.Now.Ticks);
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual ActionResult Success( string message, object data)
        {
            return Success(  message, data, null, DateTime.Now.Ticks);
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <param name="extras"></param>
        /// <returns></returns>
        protected virtual ActionResult Success(  string message, object data, object extras)
        {
            return Success( message, data, extras, DateTime.Now.Ticks);
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <param name="extras"></param>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        protected virtual ActionResult Success(string message,   object? data, object? extras,long timestamp)
        {
            RestfulResult restfulResult = new RestfulResult()
            {
                Code = HttpStatusCode.OK,
                Message = message,
                Data = data,
                Extras = extras,
                Timestamp = timestamp
            };
            return Content(restfulResult.Json2Str());
        }
        #endregion

        #region Fail
        /// <summary>
        /// 返回失败消息
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual ActionResult Fail(string message)
        {
            return Fail(message, null, DateTime.Now.Ticks);
        }

        /// <summary>
        /// 返回失败消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="extras"></param>
        /// <returns></returns>
        protected virtual ActionResult Fail(string message, object extras)
        {
            return Fail(message, extras, DateTime.Now.Ticks);
        }

        /// <summary>
        /// 返回失败消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="extras"></param>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        protected virtual ActionResult Fail(string message,object? extras,long timestamp)
        {
            RestfulResult restfulResult = new RestfulResult()
            {
                Code =  HttpStatusCode.InternalServerError,
                Message = message,
                Data = null,
                Extras = extras,
                Timestamp = timestamp
            };
            return Content(restfulResult.Json2Str());
        }
        #endregion

        #endregion
    }
}
